package com.smartstate.ems.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.smartstate.EnvironmentMonitor.vo.AlarmPageVo;
import com.smartstate.ems.dto.PageDTO;
import com.smartstate.ems.entity.RateCalculation;
import com.smartstate.ems.mapper.RateCalculationMapper;
import com.smartstate.ems.service.RateCalculationService;
import com.smartstate.ems.util.RateUtils;
import com.smartstate.ems.util.UserUtils;
import com.smartstate.ems.vo.RateCalculationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>费率</p>
 * <p>@Author 陈永杰</p>
 * <p>@Date: 2022/8/25</p>
 * <p>@Time: 16:53</p>
 * <p>@version 1.0 <p>
 */
@Service
public class RateCalculationImpl extends ServiceImpl<RateCalculationMapper, RateCalculation> implements RateCalculationService {

    @Autowired
    private RateCalculationMapper rateCalculationMapper;


    @Override
    public Page<RateCalculation> queryAllPageByDTO(PageDTO pageDTO) {
        return null;
    }


    /**
     * 增加费率信息  判断峰值 平值 谷值
     *
     * @param
     * @return
     */
    @Override
    public int addRatecalculation(RateCalculationVo rateCalculationVo) {
        RateCalculation rateCalculation = new RateCalculation();
        //生成id
        String id = IdUtil.simpleUUID();
        rateCalculation.setId(id);
        //生效时间拿到
        rateCalculation.setEffectiveTime(rateCalculationVo.getEffectiveTime());
        //拿到介质
        rateCalculation.setMedium(rateCalculationVo.getMedium());

        rateCalculation.setCreatedName(UserUtils.getLoginUser().getUsername());

        //区分峰 谷 平
        RateUtils rateUtils = new RateUtils();
        String date =rateUtils.RateAddOrUpdate(rateCalculationVo.getDetailed());

        rateCalculation.setDetailed(date);

        return this.baseMapper.addRatecalculation(rateCalculation);

    }

    @Override
    public int updateRate(RateCalculationVo rateCalculationVo) {
        RateCalculation rateCalculation = new RateCalculation();
        if (rateCalculationVo.getId()==null && "".equals(rateCalculationVo.getId())){
            //生成id
            String id = IdUtil.simpleUUID();
            rateCalculation.setId(id);

            //生效时间拿到
            rateCalculation.setEffectiveTime(rateCalculationVo.getEffectiveTime());
            //拿到介质
            rateCalculation.setMedium(rateCalculationVo.getMedium());

            rateCalculation.setCreatedName(UserUtils.getLoginUser().getUsername());

            //区分峰 谷 平
            RateUtils rateUtils = new RateUtils();
            String date =rateUtils.RateAddOrUpdate(rateCalculationVo.getDetailed());
            rateCalculation.setDetailed(date);
            return this.baseMapper.addRatecalculation(rateCalculation);
        }
        rateCalculation.setId(rateCalculationVo.getId());
        //生效时间拿到
        rateCalculation.setEffectiveTime(rateCalculationVo.getEffectiveTime());
        //拿到介质
        rateCalculation.setMedium(rateCalculationVo.getMedium());

        rateCalculation.setCreatedName(UserUtils.getLoginUser().getUsername());

        //区分峰 谷 平
        RateUtils rateUtils = new RateUtils();
        String date =rateUtils.RateAddOrUpdate(rateCalculationVo.getDetailed());
        rateCalculation.setDetailed(date);
        return  this.baseMapper.updateRate(rateCalculation);
    }


}